<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width"/>
    <title>HELLO，AMAP!</title>
    <style>
        html,
        body,
        #container {
            width: 100%;
            height: 100%;
        }
    </style>
</head>
<body>

<div id="container"></div>
</body>
</html>

<!-- 需要设置元素的宽高样式 -->
<script type="text/javascript">
    window._AMapSecurityConfig = {
        securityJsCode: "18126c12cf447573ea9216ae19829993",
    };
</script>
<script type="text/javascript" src="https://webapi.amap.com/maps?v=2.0&key=e3c0f3854e1abb83c1726ea0f94f0ac0"></script>
<script type="text/javascript">
    //地图初始化应该在地图容器 <div> 已经添加到 DOM 树之后
    const map = new AMap.Map("container", {
        viewMode: "2D", //默认使用 2D 模式
        zoom: 11, //地图级别
        center: [116.397428, 39.90923], //地图中心点
    });

    //窗体加载完成后执行的方法
    window.onload = function() {
        window.swift.setCurrentLocation() //调用鸿蒙端方法进行定位
    }

    function setLocation(lon, lat) {
        window.swift.alert('网页调用鸿蒙端方法显示坐标:' + lon + ',' + lat)
        //设置当前位置
        map.setZoomAndCenter(18, [lon, lat])
        //设置当前位置图标
        const content =
            `<div class="custom-content-marker"><img src="https://a.amap.com/jsapi_demos/static/demo-center/icons/dir-via-marker.png"></div>`;
        const marker = new AMap.Marker({
            content: content, //自定义点标记覆盖物内容
            position: [lon, lat], //基点位置
            offset: new AMap.Pixel(-13, -30), //相对于基点的偏移位置
        });
        map.add(marker);

        //搜索最近的几个地点
        map.plugin(['AMap.PlaceSearch'], function() {
            window.swift.toast('map.plugin已执行')
            var PlaceSearchOptions = { //设置PlaceSearch属性
                pageSize: 4, //每页结果数,默认10
                pageIndex: 1, //请求页码，默认1
                map: map,
                extensions: "base" //返回信息详略，默认为base（基本信息）
            };

            var MSearch = new AMap.PlaceSearch(PlaceSearchOptions); //构造PlaceSearch类
            AMap.event.addListener(MSearch, "complete", (result)=>{
                window.swift.toast('AMap.event.addListener已执行')
                window.swift.receiveData(JSON.stringify(result?.poiList?.pois || []))
            }); //返回结果
            MSearch.search("村", new AMap.LngLat(lon, lat)); //关键字查询
        });
}
</script>